package camid.distribution.lifecycle;

import camid.distribution.activeobject.ObjectRegistry;
import camid.distribution.events.FutureNotificator;
import camid.distribution.lifecycle.perclient.PerClientLifecycleManager;
import camid.distribution.lifecycle.perrequest.PerRequestLifecycleManager;

public class LifecycleBuilder {

	private ObjectRegistry registry;
	
	private PerRequestLifecycleManager perRequestManager = null;
	private PerClientLifecycleManager perClientManager = null;
	private StaticLifecycleManager staticManager = null;
	
	public LifecycleBuilder(ObjectRegistry objectRegistry, FutureNotificator futureNotificator) {
		this.registry = objectRegistry;
		this.perRequestManager = new PerRequestLifecycleManager(registry, futureNotificator);
		this.perClientManager = new PerClientLifecycleManager(registry, futureNotificator);
		this.staticManager = new StaticLifecycleManager(registry, futureNotificator);
	}

	public AbstractLifecycleManager fetchLifecycleManager(Class<?> entityClass) 
	{
		
		AbstractLifecycleManager alm = null;
		ManagedObject managedObject = entityClass.getAnnotation(ManagedObject.class);
		switch(managedObject.lifecycleScope())
		{
			case PER_REQUEST:
			{
				alm = perRequestManager;
				break;
			}
			case PER_CLIENT:
			{
				alm = perClientManager;
				break;
			}
			case STATIC:
			{
				alm = staticManager;
				break;
			}
		}
		return alm;
	}
	
	public void shutdown()
	{
		this.perClientManager.shutdown();
		this.perRequestManager.shutdown();
		this.staticManager.shutdown();
		this.registry.shutdown();
	}

}
